package com.wfit.service.impl;

import com.wfit.context.UserContext;
import com.wfit.dto.PayDto;
import com.wfit.enumeration.OrderStatusEnum;
import com.wfit.mapper.AppUsersMapper;
import com.wfit.mapper.OrderMapper;
import com.wfit.po.AppUsers;
import com.wfit.po.Order;
import com.wfit.result.Result;
import com.wfit.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private AppUsersMapper appUsersMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public Result submitPay(PayDto dto) {
        Order order = orderMapper.selectById(dto.getOrderId());
        if (order == null) {
            return Result.error("订单不存在或者订单已过时");
        }
        order.setReceiver(dto.getRecipient());
        order.setPhone(dto.getPhone());
        order.setAddress(dto.getAddress());
        order.setStatus(OrderStatusEnum.PENDING.getStatus());
        // TODO
//        order.setTruePrice(order.getTruePrice() - );
        orderMapper.updateById(order);
        AppUsers users = appUsersMapper.selectById(UserContext.getUser());
        if (dto.getPayType().equalsIgnoreCase("1")) {
            users.setAmount(users.getAmount() - order.getTruePrice());
        }
        appUsersMapper.updateById(users);
        return Result.success("支付成功！！");
    }
}
